import subprocess

import py3func
from executor.abstractcmd import NonKeyCmd

LOG = py3func.make_logging(__file__)

class VirtualShell(NonKeyCmd):
    def __new__(cls):
        if not hasattr(cls, "singleton_instance"):
            cls.singleton_instance = super().__new__(cls)
        return cls.singleton_instance  # 返回单例对象

    def __init__(self):
        # super().__init__()
        pass

    def update_command(self, str_cmd):
        self.cmd = str_cmd

    def cmd_exec(self):
        try:
            # use from python v3.5
            subprocess.run(self.cmd, shell=True, check=True)
        except AttributeError:
            # depressed after python v3.5
            subprocess.call(self.cmd, shell=True)
        except Exception:
            LOG.error("Fail to run the cmd[%s]" % self.cmd)
